home *** CD-ROM | disk | FTP | other *** search
/ System Booster / System Booster.iso / Archives / ARexxTools / fpl70.lha / src / liballoc.a < prev    next >
Encoding:
Text File  |  1994-02-05  |  7.7 KB  |  261 lines

  1.  ******************************************************************************
  2.  *              FREXX PROGRAMMING LANGUAGE                  *
  3.  ******************************************************************************
  4.  
  5.  * liballoc.a
  6.  
  7.  * Stack allocation/checking/expanding routines.
  8.  
  9.  * Authors: Kjell Ericson and Daniel Stenberg
  10.  
  11.  ******************************************************************************
  12.  
  13.  ************************************************************************
  14.  *                                                                      *
  15.  * fpl.library - A shared library interpreting script langauge.         *
  16.  * Copyright (C) 1992-1994 FrexxWare                                    *
  17.  * Author: Daniel Stenberg                                              *
  18.  *                                                                      *
  19.  * This program is free software; you may redistribute for non          *
  20.  * commercial purposes only. Commercial programs must have a written    *
  21.  * permission from the author to use FPL. FPL is *NOT* public domain!   *
  22.  * Any provided source code is only for reference and for assurance     *
  23.  * that users should be able to compile FPL on any operating system     *
  24.  * he/she wants to use it in!                                           *
  25.  *                                                                      *
  26.  * You may not change, resource, patch files or in any way reverse      *
  27.  * engineer anything in the FPL package.                                *
  28.  *                                                                      *
  29.  * This program is distributed in the hope that it will be useful,      *
  30.  * but WITHOUT ANY WARRANTY; without even the implied warranty of       *
  31.  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.                 *
  32.  *                                                                      *
  33.  * Daniel Stenberg                                                      *
  34.  * Ankdammsgatan 36, 4tr                                                *
  35.  * S-171 43 Solna                                                       *
  36.  * Sweden                                                               *
  37.  *                                                                      *
  38.  * FidoNet 2:201/328    email:dast@sth.frontec.se                       *
  39.  *                                                                      *
  40.  ************************************************************************
  41.  
  42.     ; LibAlloc.a
  43.     ;
  44.     ; Allocate and expands the stack at need. a6 is always the FPLBase
  45.     ; pointer when any of these routine are called!
  46.  
  47.     CSECT    LibAlloc
  48.  
  49.     xdef    _GetStackSize    ; long GetStackSize(struct Data *);
  50.     xdef    _GetStackUsed    ; long GetStackUsed(struct Data *);
  51.     xdef    _InitStack    ; void InitStack(struct Data *)
  52.     xdef    _EndStack    ; void EndStack(struct Data *, long maxstack)
  53.     xdef    _CheckStack    ; long OK= CheckStack(struct Data *,
  54.                 ;              long maxstack,
  55.                 ;              long minstack);
  56.     xdef    _InterfaceCall    ; long InterfaceCall(struct Data *,
  57.                 ;             void **);
  58.                 ;             func pointer);
  59.     xdef    _StoreRegisters ; void StoreRegisters(stuct Data *);
  60.     xref    _Script        ; call this function!
  61.  
  62.     xref    _Malloc        ; malloc routine
  63.     xref    _Free        ; free routine
  64.  
  65.     IFD    LOCKFUNTIONS_WANTED
  66.  
  67.     xdef    _Locker        ; void Locker(long *data, char bit);
  68.     xdef    _Unlocker    ; void Unlocker(long *data, char bit);
  69.  
  70.     ENDC
  71.  
  72.     include "exec/memory.i"
  73.     include "liballoc.i"
  74.  
  75. asALLOCSTACK equ 5000    ; Number of bytes added to current size when reallocing
  76. asINITCOPY equ 100    ; Amout of memory copied from stack (even 4)
  77. MALLOC_STATIC equ 1    ; Static allocation type specifier
  78.  
  79. _InitStack: ; ( a lot of data is puched on the stack )
  80.     movem.l    d0/d1/a0/a1,-(a7)
  81.     move.l    20(a7),a1    ; struct Data *
  82.     move.l    (a1),a0        ; new stack address
  83.  
  84.     move.l    16(a7),8(a0)
  85.     move.l    a7,4(a0)        ; Store old stackpointer
  86.     add.l    (a0),a0            ; Copy the last part of the stack
  87.     sub.l    #asINITCOPY+20,a0
  88.     move.l    #asINITCOPY,d1
  89. aslo1:    move.l    0(a7,d1),d0
  90.     move.l    d0,0(a0,d1)
  91.     sub.l    #4,d1
  92.     bpl.s    aslo1
  93.  
  94.     move.l    a0,a7
  95.     movem.l    (a7)+,d0/d1/a0/a1
  96.  
  97.     jmp    _Script
  98.  
  99.  
  100. _EndStack: ; (struct Data * - A0; max stack left - D0)
  101.     movem.l    d2/d1/a3/a4,-(a7)
  102.  
  103.     move.l    a0,a3            ; move struct Data * to A3
  104.     move.l    DATA_STACKBASE(a3),a4    ; get stack base
  105.     move.l    4(a4),a1        ; Get the old stack pointer
  106.     ; subq.l    #4,a1 ; ????????????????
  107.  
  108.     ; copy #asINITCOPY number of bytes from the FPL stack, back
  109.     ; to the previous stack to bring back the changes.
  110.  
  111.     move.l    #asINITCOPY,d1
  112. aslo2:    move.l    0(a7,d1),d2
  113.     move.l    d2,0(a1,d1)
  114.     subq.l    #4,d1
  115.     bpl.s    aslo2
  116.     move.l    a1,a7
  117.     cmp.l    (a4),d0
  118.     bhi.s    esend
  119.  
  120.     move.l    d0,-(sp)        ; push max stack left
  121.     move.l    a4,a1            ; memory address
  122.     move.b    #MALLOC_STATIC,d0    ; type
  123.     move.l    a3,a0            ; struct Data *
  124.     jsr    _Free
  125.  
  126.     move.l    (sp),d0            ; new size
  127.     move.b    #MALLOC_STATIC,d1     ; type
  128.     move.l    a3,a0            ; struct Data *
  129.     jsr    _Malloc
  130.  
  131.     move.l    (sp)+,d2        ; new size
  132.     move.l    d0,a0            ; get stack pointer
  133.     move.l    d2,(a0)            ; set size
  134.     move.l    d0,DATA_STACKBASE(a3)    ; store stack base
  135. esend:
  136.     movem.l    (a7)+,d2/d1/a3/a4
  137.     rts
  138.  
  139. _GetStackSize: ;(struct Data * - A0)
  140.     move.l    a1,-(sp)        ; backup A1
  141.     move.l    DATA_STACKBASE(a0),a1    ; stack base address
  142.     move.l    (a1),d0            ; stack size to D0
  143.     move.l    (sp)+,a1        ; restore A1
  144.     rts
  145.  
  146. _GetStackUsed: ;(struct Data * - A0)
  147.     move.l    a1,-(sp)        ; backup A1
  148.     move.l    DATA_STACKBASE(a0),a1    ; stack base address
  149.     move.l    sp,d0            ; get stack pointer to d0
  150.     sub.l    a1,d0            ; subtract stack base pointer
  151.     move.l    (sp)+,a1        ; restore A1
  152.     rts
  153.  
  154. _CheckStack: ;(struct Data * - A3; stack limit - D2 ; stack margin - D3)
  155.     movem.l    d1/a0/a1/a2,-(sp)
  156.  
  157.     move.l    DATA_STACKBASE(a3),a0    ; stack base address
  158.  
  159.     move.l    sp,d0
  160.     sub.l    a0,d0
  161.     cmp.l    d3,d0 ; have the stack usage crossed the margin?
  162.     bpl    csend ; no? return!
  163.     move.l    (a0),d0    ; get current stack size in D0
  164.     cmp.l    d0,d2    ; compare with stack limit in D2
  165.     beq    csend3    ; already using max size, go csend3!
  166.     add.l    d3,d0            ; add margin size D3 to stack size
  167.     add.l    #asALLOCSTACK,d0    ; add default stack adding space size
  168.     cmp.l    d0,d2            ; Compare with the stack limit
  169.     bpl    csla1
  170.     move.l    d2,d0            ; Use stack limit!
  171. csla1:
  172.     movem.l    d0/a0,-(sp)        ; size in D0
  173.     move.b    #MALLOC_STATIC,d1    ; type
  174.     move.l    a3,a0            ; struct Data *
  175.     jsr    _Malloc
  176.     movem.l    (sp)+,d1/a0
  177.     tst.l    d0            ; Did we get memory?
  178.     beq.s    csend2
  179.     move.l    d0,a2
  180.     move.l    d1,(a2)
  181.     move.l    4(a0),4(a2)
  182.     move.l    8(a0),8(a2)
  183.  
  184.     movem.l    a2/a3,-(sp)
  185.     move.l    a0,a1
  186.     sub.l    (a0),a2
  187.     add.l    d1,a2
  188.     move.l    (a0),d0
  189.     sub.l    #4,d0
  190. cslo1:    move.l    (a1)+,(a2)+
  191.     sub.l    #4,d0
  192.     bpl.s    cslo1
  193.  
  194.     movem.l    (sp)+,a2/a3
  195.     sub.l    a0,sp            ; Get stack use
  196.     add.l    a2,sp            ; add stack address
  197.     add.l    d3,sp            ; add margin
  198.     add.l    #asALLOCSTACK,sp    ; add stack realloc size
  199.     movem.l    a2/a3,-(sp)
  200.  
  201.             ; Deallocate old stack
  202.  
  203.     move.l    a0,a1            ; memory pointer
  204.     move.l    a3,a0            ; struct Data *
  205.     move.b    #MALLOC_STATIC,d0    ; type
  206.     jsr    _Free
  207.     movem.l    (sp)+,a2/a3
  208.  
  209.     move.l    a2,DATA_STACKBASE(a3)    ; Store the new value
  210. csend:            ; OK
  211.     moveq.l    #0,d0
  212.     movem.l    (sp)+,d1/a0/a1/a2
  213.     rts
  214. csend2:            ; OUT OF MEM
  215.     moveq.l    #1,d0
  216.     movem.l    (sp)+,d1/a0/a1/a2
  217.     rts
  218. csend3:            ; MAX STACK REACHED
  219.     moveq.l    #2,d0
  220.     movem.l    (sp)+,d1/a0/a1/a2
  221.     rts
  222.  
  223.  
  224. _InterfaceCall: ; struct Data * in A1
  225.         ; void * in A0
  226.         ; function pointer in A2
  227.  
  228.     move.l    a3,-(sp)            ; backup A3
  229.     lea    DATA_REGISTERSTORAGE(a1),a3    ; get register buffer
  230.     move.l    a2,a1                ; function pointer to A1
  231.     movem.l    d2-d7/a2/a4-a6,-(sp)        ; store registers
  232.     movem.l    (a3),d2-d7/a2-a6        ; get registers from buffer
  233.     jsr    (a1)                ; call interface function
  234.     movem.l    (sp)+,d2-d7/a2/a4-a6        ; restore old registers
  235.     move.l    (sp)+,a3            ; restore A3
  236.     rts
  237.  
  238. _StoreRegisters: ; struct Data * in A0
  239.     lea    DATA_REGISTERSTORAGE(a0),a0    ; get register buffer address
  240.     movem.l    d2-d7/a2-a6,(a0)        ; store registers in buffer
  241.     rts
  242.  
  243.     IFD    LOCKFUNCTIONS_WANTED
  244.  
  245. _Locker: ; long *data in A0
  246.      ; char bit in D0
  247.  
  248.     bset    d0,(a0)
  249.     bne.s    _Locker
  250.     rts
  251.  
  252. _Unlocker: ; long *data in A0
  253.        ; char bit in D0
  254.  
  255.     bclr    d0,(a0)
  256.     rts
  257.  
  258.     ENDC
  259.  
  260.  END
  261.